/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_TimeSwitch.
	/// </summary>
	public class Block_TimeSwitch : Block
	{

		private string _sunday;
		private string _monday;
		private string _tuesday;
		private string _wednesday;
		private string _thursday;
		private string _friday;
		private string _saturday;
		private string _goOffHookIfRequired;


		public override TYPE GetBlockType()
		{
			return TYPE.TimeSwitch;
		}


		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_TimeSwitch::GetXML Started.");	
			// <TimeSwitch name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<TimeSwitch name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			// mode=1 (just for display in studio.
			xml += " mode=\"1\"";
			// ...day0="SUNDAY_DATA"...
			xml += " day0=\"" + Utils.XmlString(_sunday) + "\"";
			// ...day0="MONDAY_DATA"...
			xml += " day1=\"" + Utils.XmlString(_monday) + "\"";
			// ...day0="TUESDAY_DATA"...
			xml += " day2=\"" + Utils.XmlString(_tuesday) + "\"";
			// ...day0="WEDNESDAY_DATA"...
			xml += " day3=\"" + Utils.XmlString(_wednesday) + "\"";
			// ...day0="THURSDAY_DATA"...
			xml += " day4=\"" + Utils.XmlString(_thursday) + "\"";
			// ...day0="FRIDAY_DATA"...
			xml += " day5=\"" + Utils.XmlString(_friday) + "\"";
			// ...day0="SATURDAY_DATA"...
			xml += " day6=\"" + Utils.XmlString(_saturday) + "\"";
			// ..._goOffHookIfRequired="GO_OFFHOOK" >
			xml += " goOffHook=\"" + Utils.XmlString(_goOffHookIfRequired) + "\">\r\n";
			// <Inputs>
			xml += GetInputsXML(_blockId);
			
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// TODO: Prompts are not written! Defaulting to dummy.
			xml += "<Working type=\"0\" rate=\"0\" duration=\"0\">" +
					"<Prompts/>" +
				"</Working>" +
				"<Closed type=\"0\" rate=\"0\" duration=\"0\">" +
					"<Prompts/>" +
				"</Closed>" +
				"<Daybrak type=\"0\" rate=\"0\" duration=\"0\">" +
					"<Prompts/>" +
				"</Daybrak>" +
				"<Weekend type=\"0\" rate=\"0\" duration=\"0\">" +
					"<Prompts/>" +
						"</Weekend>";
			// </TimeSwitch>
			xml +="\t\t\t</TimeSwitch>\r\n";
			return xml;
		}


		public Block_TimeSwitch(byte[] rawData) : base(rawData)
		{
			Trace.Debug("Block_TimeSwitch created.");

			Settings.Instance.Unimplemented("Block_TimeSwitch");
// TODO:
//			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, -1, 0));
//			outputErrorIds.Add(new BlockOutput(OUTPUT.InvalidHandle, -1, 162));
//			outputErrorIds.Add(new BlockOutput(OUTPUT.Failed_2, -1, 141));

			
			// TODO: Here should all the play-info be read.
			AddToBlockNote("This block '" + _name + "' has not been fully implemented in the decompiler. Stuff will be missing.");

			// The 'block' base class interprets the .seq file default output as
			// a 'Next' output when it in this blocktype should be 'Working', change
			// this here.
			ChangeOutputType(OUTPUT.Next, OUTPUT.Working);

			// TODO: Data here seems not to be working. Getting blank strings for ALL these in debugtrace!
			_sunday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			
			_monday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			_tuesday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			_wednesday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			_thursday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			_friday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			_saturday = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			_goOffHookIfRequired = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			Trace.Debug("SUN: " + _sunday + ", MON: " + _monday + ", TUE: " + _tuesday + 
				", WED: " + _wednesday + ", THU: " + _thursday + ", FRI: " + _friday + 
				", SAT: " + _saturday);
			Trace.Debug("GoOffHook: " + _goOffHookIfRequired);
		}
	}
}
